package com.destin.basic.exception;

/**
 * @Description:
 * @author: destin
 * @date: 2021/6/28-3:43 下午
 */
public class BusinessException extends AbstractCoderException {
    public static final int ERROR_TYPE = 2;

    public BusinessException(String message) {
        super(ErrorCode.SystemDefaultCode.BUSINESS_EXCEPTION, message, ERROR_TYPE);
    }

    private BusinessException(ErrorCode code) {
        super(code, ERROR_TYPE);
    }

    private BusinessException(ErrorCode code, String message) {
        super(code, message, ERROR_TYPE);
    }

    private BusinessException(ErrorCode code, String message, Throwable ex) {
        super(code, message, ex, ERROR_TYPE);
    }

    public static BusinessException create(String message) {
        return create(ErrorCode.SystemDefaultCode.BUSINESS_EXCEPTION, message);
    }

    public static BusinessException create(ErrorCode errorCode) {
        return new BusinessException(errorCode);
    }

    public static BusinessException create(ErrorCode errorCode, String message) {
        if (null == message) {
            return create(errorCode);
        }
        return new BusinessException(errorCode, message);
    }

    public static BusinessException create(ErrorCode errorCode, String message, Throwable exception) {
        if (null == exception) {
            return create(errorCode, message);
        }
        if (exception instanceof BusinessException) {
            BusinessException source = (BusinessException) exception;
            if (null != errorCode && errorCode != source.getErrorCode()) {
                return new BusinessException(errorCode, message, source);
            }
            return source;
        } else {
            return new BusinessException(errorCode, message, exception);
        }
    }
}
